
import 'package:development_assistant/config/db_config.dart';
import 'package:development_assistant/model/auto_test.dart';
import 'package:get/get.dart';
import 'package:sqflite/sqflite.dart';

import '../model/test_api.dart';

class TestService{
  Database database=DBConfig.getDatabase();
  String testTable="auto_test";
  String apiTable="test_api";

  Future addTest(AutoTest autoTest,List<TestApi> testApiList) async{
    var testID=await database.insert(testTable, autoTest.toMap());
    for(TestApi testApi in testApiList){
      testApi.testID=testID;
      await database.insert(apiTable, testApi.toMap());
    }
  }

  Future delTest(int testID) async{
    await database.delete(testTable,where: "test_id=?",whereArgs: [testID]);
    await database.delete(apiTable,where: "test_id=?",whereArgs: [testID]);
  }


  Future getTestList() async{
    var testApiList=[].obs;
    var testList=[].obs;
    var tests=await database.query(testTable);
    testList.value=tests;
    for(var test in tests){
      var testApi=await database.query(apiTable,where: "test_id=?",whereArgs: [test["test_id"]]);
      testApiList.add(testApi);
    }
    return [testList,testApiList];

  }
}